package com.tzl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzl.dto.StuAndCjDto;
import com.tzl.entity.Cj;
import com.tzl.entity.Students;
import com.tzl.mapper.StudentsMapper;
import com.tzl.service.ICjService;
import com.tzl.service.IStudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.PageResult;
import utils.R;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements IStudentsService {
    @Autowired
    private StudentsMapper studentsMapper;
    @Autowired
    private ICjService cjService;
    // 获取所有学生信息
    @Override
    public PageResult<Students> getAll() {
        PageResult<Students> pageResult = new PageResult<>(studentsMapper.selectList(null),
                 studentsMapper.selectCount(null) );
        System.out.println(pageResult);
        return pageResult;
    }
    // 根据页码和页面大小分页查询学生及成绩信息
    @Override
    public PageResult<StuAndCjDto> selectByPage(Integer page, Integer pageSize) {
        Page<Students> studentsPage=this.page(new Page<>(page, pageSize),null);
        PageResult<StuAndCjDto> pageResult = new PageResult<>();
        List<StuAndCjDto> rows =new ArrayList<>();
        studentsPage.getRecords().forEach(student -> {
            QueryWrapper<Cj> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sId",student.getId());
            Cj cj= cjService.getOne(queryWrapper);
            if (cj==null){
                cj=new Cj();
                cj.setClaScore(0);
            }
            StuAndCjDto stuAndcjDto = new StuAndCjDto(student.getId(),
                    student.getStuId(),student.getStuName(),student.getStuAge(),
                    cj.getClaScore());
            rows.add(stuAndcjDto);
        });
        pageResult.setRows(rows);
        pageResult.setTotal(studentsPage.getTotal());
        return pageResult;
    }
    // 根据条件分页查询学生及成绩信息
    @Override
    public PageResult<StuAndCjDto> selectByCondition(Integer page, Integer pageSize, StuAndCjDto stuAndCjDto) {
        Page<Students> page1=new Page<>(page, pageSize);
        QueryWrapper<Students> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(stuAndCjDto.getStuName()),"stuName",stuAndCjDto.getStuName());
        queryWrapper.eq(stuAndCjDto.getStuAge()>0,"stuAge",stuAndCjDto.getStuAge());

        Page<Students> students =  this.page(page1,queryWrapper);
        List<StuAndCjDto> rows =new ArrayList<>();
        students.getRecords().forEach((student)->{
            QueryWrapper<Cj> queryWrapper1 = new QueryWrapper<>();
            if(stuAndCjDto.getClaScore()==null)
                stuAndCjDto.setClaScore(0);
            queryWrapper1.eq(stuAndCjDto.getClaScore()>0,"claScore",stuAndCjDto.getClaScore());
            queryWrapper1.eq("sId",student.getId());
            Cj cj= cjService.getOne(queryWrapper1);

            if(cj==null && stuAndCjDto.getClaScore()==0){
                cj=new Cj();
                cj.setClaScore(0);
                StuAndCjDto stuAndcjDto = new StuAndCjDto(student.getId(),
                        student.getStuId(),student.getStuName(),student.getStuAge(),
                        cj.getClaScore());
                rows.add(stuAndcjDto);
            }else if (cj!=null){
                StuAndCjDto stuAndcjDto = new StuAndCjDto(student.getId(),
                        student.getStuId(),student.getStuName(),student.getStuAge(),
                        cj.getClaScore());
                rows.add(stuAndcjDto);
            }

        });
        PageResult<StuAndCjDto> pageResult = new PageResult<>(rows, (long) rows.size());
        return pageResult;
    }
    // 添加学生及成绩信息
    @Override
    public boolean addStuAndCj(StuAndCjDto stuAndCjDto) {
        Students students = new Students();
        students.setStuId(stuAndCjDto.getStuId());
        students.setStuName(stuAndCjDto.getStuName());
        students.setStuAge(stuAndCjDto.getStuAge());
        boolean save = this.save(students);
        Cj cj = new Cj();
        cj.setSId(students.getId());
        cj.setClaScore(stuAndCjDto.getClaScore());
        boolean save1 = cjService.save(cj);
        return save&save1;
    }
    // 根据id删除学生及成绩信息
    @Override
    public int deleteStuAndCj(Long id) {
        System.out.println(id);
        boolean k =this.removeById(id,false);
        QueryWrapper<Cj> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sId",id);
        cjService.remove(queryWrapper);
        return k?1:0;
    }
    // 批量删除学生及成绩信息
    @Override
    public int deleteChecked(List<String> ids) {
        int count = 0;
        if (!ids.isEmpty()){
            for (String id : ids) {
                count+=this.deleteStuAndCj(Long.parseLong(id));
            }
        }
        return count;
    }
    // 修改学生及成绩信息
    @Override
    public boolean updateStuAndCj(String id, StuAndCjDto stuAndCjDto) {
        Students students = new Students(Long.parseLong(id), stuAndCjDto.getStuId(),
                stuAndCjDto.getStuName(), stuAndCjDto.getStuAge());
        boolean k1 = this.updateById(students);
        boolean k2 ;
        UpdateWrapper<Cj> updateWrapper =  new UpdateWrapper<>();
        if(cjService.getById(Long.parseLong(id))==null){
            Cj cj = new Cj(stuAndCjDto.getClaScore(),Long.parseLong(id));
            k2=cjService.save(cj);
        }else{
            updateWrapper.eq("sId",Long.parseLong(id))
                    .set("claScore",stuAndCjDto.getClaScore());
            k2=cjService.update(updateWrapper);
        }
        return k1&k2;
    }
}
